package com.example.myapplication06;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class RegisterActivity extends AppCompatActivity {
    private EditText accountEdit;
    private EditText passwordEdit;

    private MySQLiteOpenHelper helper;
    private SQLiteDatabase database;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_register);

        helper = new MySQLiteOpenHelper(RegisterActivity.this,"androidCourse",null,1);
        database = helper.getWritableDatabase();

        accountEdit = findViewById(R.id.register_account);
        passwordEdit = findViewById(R.id.register_password);

    }

    public void register(View view) {
        String accountInput = accountEdit.getText().toString();
        String passwordInput = passwordEdit.getText().toString();

        Cursor cursor = database.query("user",null,"account=?",new String[]{accountInput},null,null,null);
        if(cursor.getCount() != 0){
            Toast.makeText(this, "账号已被注册！！", Toast.LENGTH_SHORT).show();
        }else {
            ContentValues contentValues = new ContentValues();
            contentValues.put("account",accountInput);
            contentValues.put("password",passwordInput);
            database.insert("user",null,contentValues);
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
            startActivity(intent);
        }
    }
}